package com.lmx.persistence.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.lmx.common.context.ActionContextHolder;
import com.lmx.repoDomain.tmodel.CustomerActivityServiceItem;

/**
 * 活动套餐service
 *
 * @author ytche
 * @date 2017年7月20日
 *
 */
@Service
@Scope("prototype")
public class CustomerActivityServiceItemService<T> {
	
	@PersistenceContext
	private EntityManager em;

	/**
	 * 获取getCustomerActivityPackageByName
	 */
	public CustomerActivityServiceItem getCustomerActivityServiceItemByName(String name) {
		StringBuilder query = new StringBuilder(
				"select c from CustomerActivityPackage c where c.name = ?1 and c.delStatus != ?2 and c.tenant.id=?3");
		List resultList = this.em.createQuery(query.toString()).setParameter(1, name)
				.setParameter(2, (short) 1)
				.setParameter(3, ActionContextHolder.getContext().getTenantId()).getResultList();
		CustomerActivityServiceItem u = null;
		if (resultList.size() > 0) {
			u = (CustomerActivityServiceItem) resultList.get(0);
		}
		return u;
	}
	
	/**
	 * 获取getCustomerActivityPackageByName
	 */
	public int checkServiceItemRelations(CustomerActivityServiceItem level) {
		int flag = 0;
		Long id = level.getId();
		List projectList = new ArrayList<>();
		projectList = this.em
				.createQuery(
						"select c from CustomerActivityProjectItem c where c.delStatus != ?1 and c.tenant.id=?2 and c.item.id=?3 ")
				.setParameter(1, (short) 1)
				.setParameter(2, ActionContextHolder.getContext().getTenantId())
				.setParameter(3, id).getResultList();

		flag += projectList.size();
		return flag;
	}
	
	
}
